//
//  CAPVisitorView.swift
//  CAPSwift
//
//  Created by captain on 16/4/18.
//  Copyright © 2016年 captain. All rights reserved.
//

import UIKit
import SnapKit
class CAPVisitorView: UIView {
    // MARK: - 定义一个闭包
    var visitorClosure:(() -> ())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置view信息，外界调用
    func setVisitorInfo(imgName: String?, title: String?){
        
        if let img = imgName, tit = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            
            circleImageView.hidden = true
            
        }else{
            
            setCirCleAnim()
        }
        
        
    }
    
    
    
    
    // MARK: - 点击事件
    @objc private func loginClick(){
        visitorClosure?()
    }
    
    
    // MARK: - 动画，圆圈转起来
    private func setCirCleAnim(){
        // 定义一个anim
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画执行事件
        anim.duration = 15
        // 设置toValue
        anim.toValue = 2 * M_PI
        // 动画次数
        anim.repeatCount = MAXFLOAT
        // 当我们切换tabbar 或者 程序退到后台 默认动画被移除
        anim.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(anim, forKey: nil)
    }

    // MARK: - 设置视图
    private func setupUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        // 01 添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 02 添加约束
        // 01
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        // 02
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 03
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 04
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.height.equalTo(36)
            make.width.equalTo(230)
        }
        // 05
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        // 06
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        
    }
    
    // MARK: - 懒加载
    //圆圈
    private lazy var circleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮盖
    private lazy var maskImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return img
    }()
    //小房子
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    private lazy var messageLabel:UILabel = {
        let lab = UILabel()
        
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
        lab.textAlignment = NSTextAlignment.Center
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.font = UIFont.systemFontOfSize(14)
        return lab
        
    }()
    //按钮register
    private lazy var registerButton:UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    //按钮login
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    
    
    
    
    
}
